import cv2
import numpy as np

# 读取图像
img = cv2.imread("test.jpg")  # 读取黑白效果
assert isinstance(img, np.ndarray)

img_ = np.zeros(img.shape, np.uint8)
# 非操作
img_not = cv2.bitwise_not(img)

# 或操作
img_or = cv2.bitwise_or(img, img_)

# 与操作
img_and = cv2.bitwise_and(img, img_)

# 展示图片
cv2.namedWindow("not", cv2.WINDOW_NORMAL)
cv2.resizeWindow("not", 640, 480)
cv2.imshow("not", np.hstack((img[:640, :480], img_not[:640, :480])))
cv2.namedWindow("and", cv2.WINDOW_NORMAL)
cv2.resizeWindow("and", 640, 480)
cv2.imshow("and", np.hstack((img[:640, :480], img_and[:640, :480])))
cv2.namedWindow("or", cv2.WINDOW_NORMAL)
cv2.resizeWindow("or", 640, 480)
cv2.imshow("or", np.hstack((img[:640, :480], img_or[:640, :480])))
cv2.waitKey(0)
cv2.destroyAllWindows()
